import md5 from 'js-md5'
const generateMachineIdKey = (serverCode, userName) => {
  return md5(`${serverCode}%%sx%%${userName}`)
}

export default {
  getUserInfo() {
    try {
      return JSON.parse(window.localStorage.getItem('USER_INFO')) || {}
    } catch (error) {
      console.error(error)
      return {}
    }
  },
  getUserPermission() {
    try {
      return JSON.parse(window.localStorage.getItem('USER_PERMISSION')) || []
    } catch (error) {
      return []
    }
  },
  setUserPermission(permission) {
    window.localStorage.setItem(
      'USER_PERMISSION',
      JSON.stringify(permission.map(i => i.ActionName.toUpperCase()))
    )
  },
  getTableColumnSortMap() {
    return JSON.parse(window.localStorage.getItem('USER_TABLE_SORT')) || {}
  },
  getTableColumnHideMap() {
    return (
      JSON.parse(
        window.localStorage.getItem('VXE_TABLE_CUSTOM_COLUMN_VISIBLE')
      ) || {}
    )
  },
  getTableColumnSort(tableID) {
    const columnSort = this.getTableColumnSortMap()[tableID]
    return columnSort || []
  },
  setTableColumnSort(tableID, sortList = []) {
    const tableColumnSortMap = this.getTableColumnSortMap()
    tableColumnSortMap[tableID] = sortList
    window.localStorage.setItem(
      'USER_TABLE_SORT',
      JSON.stringify(tableColumnSortMap)
    )
  },
  delTableColumnSort(tableID) {
    const tableColumnSortMap = this.getTableColumnSortMap()
    delete tableColumnSortMap[tableID]
    window.localStorage.setItem(
      'USER_TABLE_SORT',
      JSON.stringify(tableColumnSortMap)
    )
  },
  getTableHideColumn(tableID) {
    const columnHide = this.getTableColumnHideMap()[tableID]
    return columnHide || []
  },
  setTableHideColumn(tableID, hideList = []) {
    const tableHideColumnMap = this.getTableColumnHideMap()
    tableHideColumnMap[tableID] = hideList
    window.localStorage.setItem(
      'VXE_TABLE_CUSTOM_COLUMN_VISIBLE',
      JSON.stringify(tableHideColumnMap)
    )
  },
  delTableHideColumn(tableID) {
    const tableHideColumnMap = this.getTableColumnHideMap()
    delete tableHideColumnMap[tableID]
    window.localStorage.setItem(
      'VXE_TABLE_CUSTOM_COLUMN_VISIBLE',
      JSON.stringify(tableHideColumnMap)
    )
  },
  getMachineId(serverCode, userName) {
    let dataMap = window.localStorage.getItem('SX_M_CODE')
    try {
      dataMap = JSON.parse(dataMap) || {}
    } catch (error) {
      dataMap = {}
    }
    if (serverCode === undefined || userName === undefined) {
      return dataMap
    }

    return dataMap[generateMachineIdKey(serverCode, userName)]
  },
  setMachineId(serverCode, userName, id) {
    const key = generateMachineIdKey(serverCode, userName)
    const dataMap = this.getMachineId()
    dataMap[key] = id
    window.localStorage.setItem('SX_M_CODE', JSON.stringify(dataMap))
  },
  getMaintenanceVersion() {
    try {
      return (
        JSON.parse(window.localStorage.getItem('SX_MAINTENANCE_VERSION')) || {}
      )
    } catch (error) {
      console.error(error)
      return {}
    }
  },
  setMaintenanceVersion(version, hidden) {
    return window.localStorage.setItem(
      'SX_MAINTENANCE_VERSION',
      JSON.stringify({
        version,
        hidden
      })
    )
  },
  getFilterSearchConfigMap() {
    return (
      JSON.parse(window.localStorage.getItem('SX_FILTER_SEARCH_CONFIG_MAP')) ||
      {}
    )
  },
  getFilterSearchConfig(id) {
    const config = this.getFilterSearchConfigMap()[id]
    return config || {}
  },
  setFilterSearchConfig(id, configData = {}) {
    const map = this.getFilterSearchConfigMap()
    map[id] = configData
    window.localStorage.setItem(
      'SX_FILTER_SEARCH_CONFIG_MAP',
      JSON.stringify(map)
    )
  }
}
